package com.hamster.crm.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hamster.crm.mapper.MaterielConfigMapper;
import com.hamster.crm.mapper.MaterielMapper;
import com.hamster.crm.pojo.Employee;
import com.hamster.crm.pojo.Materiel;
import com.hamster.crm.pojo.MaterielConfig;
import com.hamster.crm.service.MaterielService;
import com.hamster.crm.util.IdWorker;
import com.hamster.crm.util.PageResult;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * 物料管理  Service
 * @author Tarry
 * @create 2019/12/24 10:12
 */
@Service
public class MaterielServiceImpl implements MaterielService {
    @Autowired
    private MaterielMapper materielMapper;

    @Autowired
    private MaterielConfigMapper materielConfigMapper;

    /**
     * 添加配置
     * @param materielConfig
     */
    @Override
    public void insertConfig(MaterielConfig materielConfig) {
        materielConfig.setDel("0");
        materielConfig.setInputTime(new Date());
        materielConfig.setInputUser("功能待完善");
        materielConfigMapper.insert(materielConfig);
    }

    /**
     * 删除配置
     * @param ids
     */
    @Transactional
    @Override
    public void deleteConfig(List<String> ids) {
        for (String id : ids) {
            materielConfigMapper.deleteTag(id);
        }
    }

    /**
     * 配置列表
     * @return
     */
    @Override
    public List<MaterielConfig> listConfigs() {
        QueryWrapper<MaterielConfig> queryWrapper = new QueryWrapper<>();
        queryWrapper.select("id","title");
        queryWrapper.eq("del","0");
        return materielConfigMapper.selectList(queryWrapper);
    }

    /**
     * 添加物料
     * @param materiel
     */
    @Override
    public void insertMateriel(Materiel materiel) {
//        IdWorker idWorker = new IdWorker(31,31);
//        Long id = idWorker.nextId();
//
//        materiel.setId(id.toString());
        String typeName = materielMapper.selectTypeName(materiel.getMaterielConfigId());

        materiel.setTypeName(typeName);
        materiel.setDel("0");
        materiel.setInputUser("功能待完善");
        materiel.setInputTime(new Date());

        //System.out.println(materiel);
        materielMapper.insert(materiel);
    }

    /**
     * 修改物料
     * @param materiel
     */
    @Override
    public void updateMateriel(Materiel materiel) {
        String typeName = materielMapper.selectTypeName(materiel.getMaterielConfigId());
        materiel.setTypeName(typeName);
        materiel.setInputUser("功能待完善");
        materiel.setInputTime(new Date());
        materielMapper.updateById(materiel);
    }

    /**
     * 删除物料
     * @param ids
     */
    @Transactional
    @Override
    public void deleteMateriel(List<String> ids) {
        for (String id : ids) {
            materielMapper.deleteTag(id);
        }

    }

    /**
     * 条件分页查询物料
     * @param pageSize      每页条数
     * @param currentPage   当前页码
     * @param title         物料名称
     * @return
     */
    @Override
    public PageResult<Materiel> listMateriels(Integer pageSize, Integer currentPage, String title) {
        //分页
        Page<Materiel> page = new Page<>(currentPage, pageSize);

        QueryWrapper<Materiel> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("del","0");
        if (StringUtils.isNotBlank(title)){
            queryWrapper.like("title","%"+title+"%");
        }
        IPage pageList = materielMapper.selectPage(page, queryWrapper);
        List<Materiel> currentList = pageList.getRecords();
        Long pages = pageList.getPages();
        return new PageResult<Materiel>(pageList.getTotal(),pages.intValue(),currentList);
    }

    /**
     * 导出 物料 excel
     * @return
     */
    @Override
    public List<Materiel> queryAllMateriel() {
        QueryWrapper<Materiel> materielQueryWrapper = new QueryWrapper<>();
        materielQueryWrapper.eq("del","0");
        return materielMapper.selectList(materielQueryWrapper);
    }
}
